using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace ExplorerKiller
{
    internal static class SpecialValue
    {
        // 缩略图缓存文件夹路径
        public static string ThumbCacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\Windows\\Explorer");
                
        // Windows API 常量
        public const int SW_HIDE = 0;
        public const int SW_SHOW = 5;
        public const int SPI_SETSTARTMENU = 0x1004;
        public const int SPI_SETTASKBARPOS = 0x0020;
        public const int SPIF_UPDATEINIFILE = 0x01;
        public const int SPIF_SENDCHANGE = 0x02;
        
        /// <summary>
        /// 获取缩略图缓存文件夹中所有匹配 thumbcache_*.db 和 iconcache_*.db 模式的文件
        /// </summary>
        /// <returns>匹配的文件路径数组</returns>
        public static string[] GetThumbCacheFiles()
        {
            if (!Directory.Exists(ThumbCacheFolder))
            {
                return new string[0];
            }
            
            // 创建正则表达式以匹配 thumbcache_*.db 和 iconcache_*.db 文件
            Regex cacheFileRegex = new Regex(@"^(thumbcache_|iconcache_).*\.db$", RegexOptions.IgnoreCase);
            
            List<string> matchingFiles = new List<string>();
            
            try
            {
                // 获取文件夹中的所有文件
                string[] allFiles = Directory.GetFiles(ThumbCacheFolder);
                
                // 筛选匹配正则表达式的文件
                foreach (string file in allFiles)
                {
                    string fileName = Path.GetFileName(file);
                    if (cacheFileRegex.IsMatch(fileName))
                    {
                        matchingFiles.Add(file);
                    }
                }
            }
            catch (Exception)
            {
                // 如果发生异常（如权限不足），返回空数组
                return new string[0];
            }
            
            return matchingFiles.ToArray();
        }
    }
    
    // Windows API 调用
    internal static class NativeMethods
    {
        /// <summary>
        /// 显示或隐藏指定窗口
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="nCmdShow">显示状态命令</param>
        /// <returns>操作是否成功</returns>
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        
        /// <summary>
        /// 根据类名和窗口名查找窗口
        /// </summary>
        /// <param name="lpClassName">窗口类名</param>
        /// <param name="lpWindowName">窗口标题</param>
        /// <returns>找到的窗口句柄，如果未找到则返回IntPtr.Zero</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        /// <summary>
        /// 更改系统参数
        /// </summary>
        /// <param name="uAction">要更改的参数</param>
        /// <param name="uParam">参数的新值</param>
        /// <param name="lpvParam">参数的附加信息</param>
        /// <param name="fuWinIni">更新设置的方式</param>
        /// <returns>操作是否成功</returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool SystemParametersInfo(int uAction, int uParam, IntPtr lpvParam, int fuWinIni);
        
        /// <summary>
        /// 切换到指定窗口
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="fAltTab">是否使用Alt+Tab方式切换</param>
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
        
        // 获取任务栏句柄
        public static IntPtr GetTaskbarHandle()
        {
            return FindWindow("Shell_TrayWnd", null);
        }
        
        // 显示或隐藏任务栏
        public static void ShowTaskbar(bool show)
        {
            IntPtr taskbarHandle = GetTaskbarHandle();
            if (taskbarHandle != IntPtr.Zero)
            {
                ShowWindow(taskbarHandle, show ? SpecialValue.SW_SHOW : SpecialValue.SW_HIDE);
            }
        }

        /// <summary>
        /// 在指定窗口的子窗口中查找指定类名和窗口名的窗口
        /// </summary>
        /// <param name="hWndParent">父窗口句柄</param>
        /// <param name="hWndChildAfter">要开始搜索的子窗口句柄</param>
        /// <param name="lpszClass">窗口类名</param>
        /// <param name="lpszWindow">窗口标题</param>
        /// <returns>找到的窗口句柄，如果未找到则返回IntPtr.Zero</returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);

        /// <summary>
        /// 检查窗口是否可见
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <returns>窗口是否可见</returns>
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        // 显示或隐藏桌面窗口（而非任务栏）
        public static void ShowDesktopIcons(bool show)
        {
            try
            {
                // 查找Program Manager窗口
                IntPtr progmanHandle = FindWindow("Progman", "Program Manager");
                if (progmanHandle != IntPtr.Zero)
                {
                    // 查找SHELLDLL_DefView子窗口
                    IntPtr shellViewHandle = FindWindowEx(progmanHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
                    if (shellViewHandle != IntPtr.Zero)
                    {
                        // 显示或隐藏桌面窗口
                        ShowWindow(shellViewHandle, show ? SpecialValue.SW_SHOW : SpecialValue.SW_HIDE);
                    }
                }
            }
            catch { }
        }
    }
}